<?php
class mis_komplain_model extends MY_Model {
	var $model = 'mis_komplain_model';
	
	function ISPStatus()
	{
		$ls_sql	= "	SELECT	NAMA_STATUS value,
							NAMA_STATUS display
					FROM	MIS_KOMPLAIN_STATUS
					ORDER BY NAMA_STATUS";
					
		$result	= $this->db->query($ls_sql);
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return array();
		}
	}
	
	function Departemen()
	{
		$ls_sql	= "	SELECT	id_departemen_pk value,
							nama_departemen display
					FROM	hrd_departemen
					ORDER BY id_departemen_pk";
					
		$result	= $this->db->query($ls_sql);
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return array();
		}
	}

	function GetJSON()
	{
		$ls_sql	= "	SELECT	a.id_komplain,a.atas_nama,a.kode_komplain,TO_CHAR(a.tgl_komplain,'DD/MM/YYYY') tgl_komplain,
							a.nama_unit,a.komplain,a.status,a.keterangan,b.nama_departemen,
							a.jenis_kebutuhan,
							a.jenis_permintaan,
							TO_CHAR(A.tanggal_selesai,'DD/MM/YYYY') tanggal_selesai
					FROM	mis_komplain a
					LEFT JOIN hrd_departemen b ON a.nama_unit = b.id_departemen_pk
					WHERE	(UPPER(a.kode_komplain) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(b.nama_departemen) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.tgl_komplain) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.status) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.nama_unit) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.keterangan) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.komplain) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'a.id_komplain' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
		
	function Add($arr_data){
		$this->db->insert('mis_komplain', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('mis_komplain', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_id_komplain){
		$result = $this->db->get_where('mis_komplain', array('id_komplain' => $as_id_komplain));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_id_komplain)
	{
		$result = $this->db->get_where('mis_komplain', array('id_komplain' => $as_id_komplain));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			if(	intval($this->get_data("SELECT COUNT(1) FROM mis_komplain WHERE id_komplain = '".$arr_rule['id_komplain']."'"))<=0)
			{
				return $this->error_delete.' [Id: '.$arr_rule['id_komplain']."]<br />";
			}else{
				$this->db->delete('mis_komplain', array('id_komplain' => $arr_rule['id_komplain']));
				return '';
			}
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}

	function GetCetak($kode_komplain){

		$result = $this->db->query("SELECT	a.id_komplain,a.atas_nama,
											a.kode_komplain,TO_CHAR(a.tgl_komplain,'DD/MM/YYYY') tgl_komplain,
											a.nama_unit,a.komplain,a.status,a.keterangan,b.nama_departemen,
											a.jenis_kebutuhan,
											a.jenis_permintaan,
											TO_CHAR(A.tanggal_selesai,'DD/MM/YYYY') tanggal_selesai
									FROM	mis_komplain a
									LEFT JOIN hrd_departemen b ON a.nama_unit = b.id_departemen_pk
									WHERE  a.kode_komplain = '".$kode_komplain."' ");

		return $result->row_array();
	}
}
?>